<template>
  <view class="login-container">
    <!-- 提示登录图标 -->
    <uni-icons type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
    <!-- 登录按钮 -->
    <button type="primary" class="btn-login" @click="aaa">一键登录</button>
    <!-- 登录提示 -->
    <view class="tips-text">登录后尽享更多权益</view>
  </view>
</template>

<script>
  import {
    mapMutations,
    mapState
  } from 'vuex'

  export default {
    name: "my-login",
    data() {
      return {};
    },
    methods: {
      ...mapMutations('m_user', ['updateUserInfo', 'updateToken','updateRedirectInfo']),
      // getUserInfo(e) {
      //   console.log(e);
      //   if (e.detail.errMsg === 'getUserInfo:fail auth deny') return uni.$showMsg('您取消了登录授权')
      //   this.updateUserInfo(e.detail.userInfo)
      //   // 获取登录成功后的 Token 字符串
      //   this.getToken(e.detail)
      // },
      aaa() {
        // 获取用户信息
        // uni官方 微信一键登录
        uni.getUserInfo({
          desc: '请求授权',
          success: (res) => {
            // 存储微信登录信息
            this.updateUserInfo(res)
            // console.log("微信登录信息",res);
            // 获取登录成功后的 Token 字符串
            this.getToken(res)
          },
          fail: (res) => {
            console.log(res);
            return uni.$showMsg('已取消登录授权')
          }
        })
      },
      // 调用登录接口，换取永久 token
      async getToken(info) {
        // 获取微信的code
        const [err, res] = await uni.login().catch(err => err)
        // console.log(err, res);
        if (err || res.errMsg !== 'login:ok') return uni.$showMsg('登录失败！')

        // 参数对象
        const query = {
          code: res.code,
          encryptedData: info.encryptedData,
          iv: info.iv,
          rawData: info.rawData,
          signature: info.signature
        }
        console.log(query);
        // 换取 token
        const data = await uni.$http.post('/api/public/v1/users/wxlogin', query)
        console.log('data:', data);
        let loginResult=data.data
        if (loginResult.meta.status !== 200) uni.$showMsg('登录失败! 因为接口原因，所有直接设置了默认token')
        // uni.$showMsg('登录成功!')
        // this.updateToken(loginResult.message.token)
        // 调用仓库的方法，把token
        this.updateToken("Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEyLCJpYXQiOjE1MjU0MDIyMjMsImV4cCI6MTUyNTQ4ODYyM30.g-4GtEQNPwT_Xs0Pq7Lrco_9DfHQQsBiOKZerkO-O-o")
        
        this.navigateBack()
      },
      navigateBack(){
        if(this.redirectInfo && this.redirectInfo.openType==='switchTab'){
          uni.switchTab({
            url:this.redirectInfo.from,
            complete:() => {
              this.updateRedirectInfo(null)
            }
          })
        }
      }
    },
    computed:{
      ...mapState('m_user',['redirectInfo'])
    }
  }
</script>

<style lang="scss">
  .login-container {
    height: 750rpx;
    background-color: #e5e5e5;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    position: relative;
    overflow: hidden;

    &::after {
      content: ' ';
      display: block;
      width: 100%;
      height: 40px;
      background-color: white;
      position: absolute;
      bottom: 0;
      left: 0;
      border-radius: 100%;
      transform: translateY(50%);

    }

    .btn-login {
      width: 90%;
      border-radius: 100px;
      margin: 15px 0;
      background-color: #c00000;
    }

    .tips-text {
      font-size: 12px;
      color: gray;
    }
  }
</style>